package com.td.module.farmer.controller.admin.plantinginformation;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.farmer.controller.admin.plantinginformation.vo.*;
import com.td.module.farmer.convert.plantinginformation.PlantingInformationConvert;
import com.td.module.farmer.dal.dataobject.plantinginformation.PlantingInformationDO;
import com.td.module.farmer.service.plantinginformation.PlantingInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 种植信息")
@RestController
@RequestMapping("/farmer/planting-information")
@Validated
public class PlantingInformationController {

    @Resource
    private PlantingInformationService plantingInformationService;

    @PostMapping("/create")
    @ApiOperation("创建种植信息")
    @PreAuthorize("@ss.hasPermission('farmer:planting-information:create')")
    public CommonResult<Long> createPlantingInformation(@Valid @RequestBody PlantingInformationCreateReqVO createReqVO) {
        return success(plantingInformationService.createPlantingInformation(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新种植信息")
    @PreAuthorize("@ss.hasPermission('farmer:planting-information:update')")
    public CommonResult<Boolean> updatePlantingInformation(@Valid @RequestBody PlantingInformationUpdateReqVO updateReqVO) {
        plantingInformationService.updatePlantingInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除种植信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('farmer:planting-information:delete')")
    public CommonResult<Boolean> deletePlantingInformation(@RequestParam("id") Long id) {
        plantingInformationService.deletePlantingInformation(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得种植信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('farmer:planting-information:query')")
    public CommonResult<PlantingInformationRespVO> getPlantingInformation(@RequestParam("id") Long id) {
        PlantingInformationDO plantingInformation = plantingInformationService.getPlantingInformation(id);
        return success(PlantingInformationConvert.INSTANCE.convert(plantingInformation));
    }

    @GetMapping("/list")
    @ApiOperation("获得种植信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('farmer:planting-information:query')")
    public CommonResult<List<PlantingInformationRespVO>> getPlantingInformationList(@RequestParam("ids") Collection<Long> ids) {
        List<PlantingInformationDO> list = plantingInformationService.getPlantingInformationList(ids);
        return success(PlantingInformationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得种植信息分页")
    public CommonResult<PageResult<PlantingInformationRespVO>> getPlantingInformationPage(@Valid PlantingInformationPageReqVO pageVO) {
        PageResult<PlantingInformationDO> pageResult = plantingInformationService.getPlantingInformationPage(pageVO);
        return success(PlantingInformationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/sum")
    @ApiOperation("统计数量和地块面积")
    public CommonResult<PlantingInformationSumVO> sumPlantingInformation() {
        PlantingInformationSumVO  sum = plantingInformationService.sumLandAndArea();
        return success(sum);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出种植信息 Excel")
    @PreAuthorize("@ss.hasPermission('farmer:planting-information:export')")
    @OperateLog(type = EXPORT)
    public void exportPlantingInformationExcel(@Valid PlantingInformationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PlantingInformationDO> list = plantingInformationService.getPlantingInformationList(exportReqVO);
        // 导出 Excel
        List<PlantingInformationExcelVO> datas = PlantingInformationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "种植信息.xls", "数据", PlantingInformationExcelVO.class, datas);
    }

}
